﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MethodIT.Helper;
using MyGeneration.dOOdads;

using Hr20.DAL;
using Hr20.BLL;
using Hr20.ProcessFlow;

namespace Appraisal.Web.ZB
{
    public partial class NewCheckTask : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int ID = HelperURL.GetIntFromUrlString("ID");
                int SubID = HelperURL.GetIntFromUrlString("SubID");
                string Mode = HelperURL.GetStrFromUrlString("mode");
                if (Mode == "new")
                {
                    newform(SubID);
                }
                else
                {
                    LoadData(ID);
                }
            }
        }

        private void newform(int ID)
        {
            KP_Subject s = new KP_Subject();
            if (s.LoadByPrimaryKey(ID))
            {
                labSubjectName.Text = s.SubjectName;
                labCreater.Text = DBController.GetInstance().GetAuthenticatedUserName();
                labCreateDate.Text = DateTime.Now.ToShortDateString();
            }
            p.Visible = false;
            btnEnable.Enabled = false;
        }

        private void LoadData(int ID)
        {
            string sUserID = DBController.GetInstance().GetAuthenticatedUserID();
            ZB_Check c = new ZB_Check();
            if (c.LoadByPrimaryKey(ID))
            {
                KP_Subject s = new KP_Subject();
                s.LoadByPrimaryKey(c.SubID);
                labSubjectName.Text = s.SubjectName;
                labCreater.Text = DBController.GetInstance().GetUserName(c.Creater);
                labCreateDate.Text = DateTime.Now.ToShortDateString();
                txtDesc.Text = c.CheckDesc;
                txtReMark.Text = c.Remark;
                FunctionSet.SetListIndex(dplResult, c.Result);
            }
            txtDesc.Attributes.Add("readonly", "readonly");
            p.Visible = true;
            btnSave.Enabled= false;
        }

        private ZB_Check getData()
        {
            int ID = HelperURL.GetIntFromUrlString("ID");
            int SubID = HelperURL.GetIntFromUrlString("SubID");
            string sUserID = DBController.GetInstance().GetAuthenticatedUserID();
            ZB_Check c = new ZB_Check();
            if (!c.LoadByPrimaryKey(ID))
            {
                c.AddNew();
                c.SubID = SubID;
                c.CheckType = "项目修改申请";
                c.CheckDesc = txtDesc.Text;
                c.Creater = sUserID;
                c.CreateDate = DateTime.Now; 
            }
            return c;
        }

        protected void lbSave_Click(object sender, EventArgs e)
        {
            ZB_Check c = getData();
            c.Enable = "-";
            c.Save();

            string sURL = "../Appraisal/SubjectDisplay.aspx?ID=" + c.SubID;
            HelperURL.Redirect(sURL);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            int SubID = HelperURL.GetIntFromUrlString("SubID");
            string sURL = "../Appraisal/SubjectDisplay.aspx?ID=" + SubID;
            HelperURL.Redirect(sURL);
        }

        protected void btnEnable_Click(object sender, EventArgs e)
        {
            string sUserID = DBController.GetInstance().GetAuthenticatedUserID();
            if (!DBController.GetInstance().CheckUserRole(sUserID, "DeptAdmin"))
            {
                HelperMessage.Message("没有操作权限!");
                return;
            }
            ZB_Check c = getData();
            c.Checker = sUserID;
            c.CheckDate = DateTime.Now;
            c.Remark = txtReMark.Text;
            c.Enable = dplResult.SelectedValue ;
            if (c.Enable.Equals("Y"))
            {
                KP_Subject s = new KP_Subject();
                s.LoadByPrimaryKey(c.SubID);
                s.Enable = "N";
                s.Save();
            }

            c.Save();

            HelperMessage.Message("审核完成！");
            JScriptSet.Close();
        }
    }
}
